<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="orders"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column type="index" :index="indexComputed" label="序号" width="55px"></el-table-column>
      <el-table-column prop="id" label="工单号" width="100px" sortable></el-table-column>
      <el-table-column prop="questionTime" label="最近提问时间" width="180px" sortable></el-table-column>
      <el-table-column prop="question" label="最近提问" width="250px"></el-table-column>
      <el-table-column prop="answerTime"  label="最近答复时间" width="180px" sortable></el-table-column>
      <el-table-column prop="answer"  label="最近回答" width="250px"></el-table-column>
      <el-table-column 
        prop="status"  
        label="状态" 
        :filters="[{ text: '待回复', value: 0 }, { text: '已回复', value: 1 },{ text: '已终结', value: 2 }]"
        :filter-method="filterStatus"
        filter-placement="bottom-end"
        width="100px">
        <template slot-scope="scope">
        <el-tag
          :type="tagType(scope.row)"
          disable-transitions>{{formatStatus(scope.row)}}</el-tag>
      </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="messageRecords(scope.row)"
            type="text"
            size="small"
          >消息记录</el-button>
          <el-button
            @click="end(scope.row)"
            type="text"
            size="small"
          >终结</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { getOrders, updateOrder} from '@/api/order'
import { getInfo } from '@/api/user'
export default {
  filters: {
    // statusFilter(status) {
    //   const statusMap = {
    //     published: 'success',
    //     draft: 'gray',
    //     deleted: 'danger'
    //   }
    //   return statusMap[status]
    // }
  },
  data() {
    return {
      orders: [],
      listLoading: true,
      pageSize: 10,
      currentPage: 1,
      total: 0
    }
  },
  computed: {
    indexComputed() {
      return (this.currentPage - 1) * this.pageSize + 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getInfo().then(response => {
        const userVo = response.data
        this.listLoading = true
        getOrders({
            uid: userVo.id, 
            status: [0, 1],
            pageNum: this.currentPage,
            pageSize: this.pageSize
          }).then(response => {
          this.orders = response.data.orders
          this.total = response.data.total
          this.listLoading = false
        })
      })
    },
    messageRecords(row) {
      this.$router.push({
        path: '/orderSystem/orderProcess',
        query: {
          orderId: row.id,
          status: row.status
        }
      })
    },
    async end(row){
      const { message } = await updateOrder({id: row.id, status: 2});
      this.$message({
            message: message,
            type: 'success'
          })
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    formatStatus(row, column) {
      switch(row.status){
        case 0:
          return "待回复";
        case 1:
          return "已回复";
        case 2:
          return "已终结"
      }
    },
    filterStatus(value, row){
      return row.status == value
    },
    tagType(row){
      switch(row.status){
        case 0:
          return 'primary';
        case 1:
          return 'success';
        case 2:
          return 'danger';
      }
    }
  }
}
</script>
